package com.sfh.cp.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cp.R;
import com.sfh.cp.mass.MassAddContentActivity;
import com.sfh.cp.util.UtilFace;

import crm.sfh.server.entity.ENote;

/**
 * 发布内容-文字形式
 */

public class WidgetContentTextLayout extends LinearLayout implements View.OnClickListener {

    TextView tvContent;
    TextView tvLabel;

    TextView tvReading;
    TextView tvFabulous;
    TextView tvMessage;

    IContentOnClickLayout onClickLayout;
    ENote content;

    public WidgetContentTextLayout(Context context) {
        super(context);
        this.init();
    }

    public WidgetContentTextLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public WidgetContentTextLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }

    private void init() {
        inflate(this.getContext(), R.layout.widget_content_text_layout, this);
        this.tvContent = this.find(R.id.tvContent);
        this.tvLabel = this.find(R.id.tvLabel);

        this.tvReading = this.find(R.id.tvReading);
        this.tvFabulous = this.find(R.id.tvFabulous);
        this.tvMessage = this.find(R.id.tvMessage);

        this.tvFabulous.setOnClickListener(this);

        this.setTitltmaxLines(5);
    }

    private <T> T find(int r) {
        return (T) findViewById(r);
    }

    /**
     * 设置数据
     */
    public void setData(ENote content) {
        this.content = content;
        if (content == null){
            return;
        }
        SpannableString spannableString = UtilFace.getEmotionContent(getContext (), this.tvContent, content.getTitle ());
        this.tvContent.setText(spannableString);
        this.tvLabel.setVisibility(TextUtils.isEmpty(content.getLable()) ? GONE : VISIBLE);
        this.tvLabel.setText(String.format("〇 %s",content.getLable()));

        this.tvReading.setText(String.format("浏览%s次", content.getSizeReading()));
        this.tvFabulous.setText(content.getSizeFabulous()+"");
        this.tvMessage.setText(content.getSizeMessage()+"");

        this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(this.content.isFabulous()?R.mipmap.like_select:R.mipmap.like,0,0,0);

    }

    public void setTitltmaxLines(int maxLines) {
        this.tvContent.setMaxLines(maxLines);
        this.tvContent.setEllipsize(TextUtils.TruncateAt.END);
    }

    public void setOnClickLayout(IContentOnClickLayout onClickLayout) {
        this.onClickLayout = onClickLayout;
    }

    @Override
    public void onClick(View v) {
        if (this.onClickLayout == null|| content == null) {
            return;
        }

        if (v == this.tvFabulous) {
            this.tvFabulous.setText(String.valueOf(content.getSizeFabulous() +1));
            this.content.setUserFabulous(true);
            this.tvFabulous.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.like_select,0,0,0);
            this.onClickLayout.onClickFabulous(v, content);
            return;
        }
    }
}
